<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="75px">
      <el-form-item label="标题/描述" prop="description">
        <el-input
          v-model="queryParams.description"
          placeholder="请输入标题/描述"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="账号昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入账号昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="短标题" prop="shortTitle">
        <el-input
          v-model="queryParams.shortTitle"
          placeholder="请输入短标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="publishRecordList"  @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题/描述" align="center" prop="description" >
        <template slot-scope="scope">
          <el-tooltip class="item" effect="light" :content="scope.row.description" placement="top">
            <span style="font-style: italic;"> {{ scope.row.description ?  scope.row.description.substr(0,5) + '...' : '未设置标题' }} </span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="账号头像" align="center" prop="nickname" >
        <template slot-scope="scope">
            <div style="position: relative;">
                    <img style="width: 50px;height: 50px;border-radius: 50px;margin:5px;" :src="scope.row.headImgUrl">
                    <img style="width: 20px;height: 20px;border-radius: 50px;position:absolute;bottom: 10px;left: 50px"
                         :src="require('@/assets/media/' + scope.row.platformCode + '.png' )">
            </div>
        </template>
      </el-table-column>
      <el-table-column label="账号昵称" align="center" width="100"  prop="nickname" />
      <el-table-column label="视频封面" align="center" prop="coverLink" >
        <template slot-scope="scope">
          <img v-if="scope.row.coverLink" :src="scope.row.coverLink" style="width: 80px;height: 80px;margin:5px;border-radius: 5px">
          <span v-else style="color: gray; font-style: italic;">暂无</span>
        </template>
      </el-table-column>

      <el-table-column label="发布状态" align="center" prop="publishStatus" >
        <template slot-scope="scope">
          <span v-if="scope.row.publishStatus === 0" style="color: #7d74f1;">待发布 ●</span>
          <span v-if="scope.row.publishStatus === 1" style="color: #16b455;">已发布 ●</span>
          <span v-if="scope.row.publishStatus === 2" style="color: #8f52cb;">发布中 ●</span>
          <span v-if="scope.row.publishStatus === -1" style="color: #ec3333;">发布失败 ●</span>
        </template>
      </el-table-column>

<!--      <el-table-column label="短标题" align="center" prop="shortTitle" />-->
<!--      <el-table-column label="地理位置" align="center" prop="cityIp" />-->
      <el-table-column label="发布时间" align="center" prop="publishTime" width="180">
        <template slot-scope="scope">
          <span>{{ timestampToTime(scope.row.publishTime) }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="视频预览" align="center" width="100"  prop="videoLink" >-->
<!--        <template slot-scope="scope">-->
<!--          <video-->
<!--            width="30px"-->
<!--            height="30px"-->
<!--            :src="scope.row.videoLink"> </video>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="创建时间" align="center" width="100"  prop="createTime" />
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="details(scope.row)"
            v-hasPermi="['media:publishRecord:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['media:publishRecord:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog :title="title" :visible.sync="openDetail" width="500px" append-to-body>
       <div style="height: 500px">
         {{detailData.publishStatus}}
       </div>
    </el-dialog>


    <!-- 添加或修改视频号发布记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker clearable
            v-model="form.publishTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账号昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入账号昵称" />
        </el-form-item>
        <el-form-item label="封面链接" prop="coverLink">
          <el-input v-model="form.coverLink" placeholder="请输入封面链接" />
        </el-form-item>
        <el-form-item label="短标题" prop="shortTitle">
          <el-input v-model="form.shortTitle" placeholder="请输入短标题" />
        </el-form-item>
        <el-form-item label="地理位置" prop="cityIp">
          <el-input v-model="form.cityIp" placeholder="请输入地理位置" />
        </el-form-item>
        <el-form-item label="标题/描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入标题/描述" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPublishRecord, getPublishRecord, delPublishRecord, addPublishRecord, updatePublishRecord } from "@/api/media/publishRecord";
import {Message} from "element-ui";
export default {
  name: "PublishRecord",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      openDetail:false,
      detailData:{},
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 视频号发布记录表格数据
      publishRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      publishVideo:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: undefined,
        userId: undefined,
        publishTime: undefined,
        publishStatus: undefined,
        nickname: undefined,
        coverLink: undefined,
        shortTitle: undefined,
        cityIp: undefined,
        description: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "部门id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        publishTime: [
          { required: true, message: "发布时间不能为空", trigger: "blur" }
        ],
        publishStatus: [
          { required: true, message: "发布状态不能为空", trigger: "change" }
        ],
        nickname: [
          { required: true, message: "账号昵称不能为空", trigger: "blur" }
        ],
        coverLink: [
          { required: true, message: "封面链接不能为空", trigger: "blur" }
        ],
        shortTitle: [
          { required: true, message: "短标题不能为空", trigger: "blur" }
        ],
        cityIp: [
          { required: true, message: "地理位置不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "标题/描述不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询视频号发布记录列表 */
    getList() {
      this.loading = true;
      listPublishRecord(this.queryParams).then(response => {
        this.publishRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },



    /**
     * 查看详情
     */
    details(row){
      this.detailData = row;
      this.openDetail = true
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        deptId: undefined,
        userId: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        publishTime: undefined,
        publishStatus: undefined,
        nickname: undefined,
        coverLink: undefined,
        shortTitle: undefined,
        cityIp: undefined,
        description: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {

      this.reset();
      this.open = true;
      this.title = "添加视频号发布记录";
    },


    /** 时间戳转换为时间格式 */
    timestampToTime(timestamp) {
      if(!timestamp){
        return '立即发布'
      }
      const date = new Date(timestamp * 1000);
      const Y = date.getFullYear() + "-";
      const M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      const D = date.getDate() < 10 ? "0" + date.getDate() + " " : date.getDate() + " ";
      const h = date.getHours() < 10 ? "0" + date.getHours() + ":" : date.getHours() + ":";
      const m = date.getMinutes() < 10 ? "0" + date.getMinutes() + ":" : date.getMinutes() + ":";
      const s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
      return Y + M + D + h + m + s;
    },



    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getPublishRecord(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改视频号发布记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updatePublishRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addPublishRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      let tips = "是否删除该视频发布记录？";
      if(row.publishStatus === 0){
        tips = "未发布的视频删除后将取消发布，是否确认删除？"
      }
      this.$modal.confirm(tips).then(() => {
        this.loading = true;
        return delPublishRecord(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('media/publishRecord/export', {
        ...this.queryParams
      }, `publishRecord_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
